/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.tree.TreeSelectionModel;

import org.jxSwing.component.panel.FileTreePanel;
import org.jxSwing.i18n.I18N;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.dialog
 * @date 14/08/2014 - 21:49:01
 */
public abstract class FileBrowserDialog extends Dialog {
	
	private static final long serialVersionUID = 9098319327146567686L;
	
	private final File root;
	
	private final boolean showHideFiles;
	
	private final boolean showOnlyDirectories;
	
	private FileTreePanel fileTreePanel;
	
	private JPanel pnlAction;
	
	private JButton btnCancel;
	
	private JButton btnOK;
	
	/**
	 * 
	 */
	public FileBrowserDialog() {
	
		this(false);
	}
	
	/**
	 * @param showHideFiles
	 */
	public FileBrowserDialog(final boolean showHideFiles) {
	
		this(null, showHideFiles, false);
	}
	
	/**
	 * @param showHideFiles
	 * @param showOnlyDirectories
	 */
	public FileBrowserDialog(final boolean showHideFiles, final boolean showOnlyDirectories) {
	
		this(null, showHideFiles, showOnlyDirectories);
	}
	
	/**
	 * @param root
	 * @param showHideFiles
	 * @param showOnlyDirectories
	 */
	public FileBrowserDialog(final File root, final boolean showHideFiles, final boolean showOnlyDirectories) {
	
		this.root = root;
		this.showHideFiles = showHideFiles;
		this.showOnlyDirectories = showOnlyDirectories;
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setTitle(I18N.component().selectDirectory());
		getContentPane().add(getFileTreePanel(), BorderLayout.CENTER);
		getContentPane().add(getPnlAction(), BorderLayout.SOUTH);
		
		setSize(350, 350);
		setModal(true);
		centralize();
	}
	
	/**
	 * @param files
	 *        never is null
	 * @param selectionModel
	 *        by default is {@link TreeSelectionModel.SINGLE_TREE_SELECTION}
	 */
	protected abstract void okAction(List<File> files, TreeSelectionModel selectionModel);
	
	/**
	 * @return the btnCancel
	 */
	public final JButton getBtnCancel() {
	
		if(btnCancel == null) {
			btnCancel = new JButton(I18N.component().cancel());
			btnCancel.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(final ActionEvent actionEvent) {
				
					exitAction();
				}
			});
		}
		return btnCancel;
	}
	
	/**
	 * @return the btnOK
	 */
	public final JButton getBtnOK() {
	
		if(btnOK == null) {
			btnOK = new JButton(I18N.component().ok());
			btnOK.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(final ActionEvent actionEvent) {
				
					okAction(getFileTreePanel().getSelectedFiles(), getSelectionMode());
					
					dispose();
				}
			});
		}
		return btnOK;
	}
	
	/**
	 * @return the fileTreePanel
	 */
	public final FileTreePanel getFileTreePanel() {
	
		if(fileTreePanel == null) {
			fileTreePanel = new FileTreePanel(root, showHideFiles, showOnlyDirectories);
			fileTreePanel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		}
		return fileTreePanel;
	}
	
	/**
	 * @return the pnlAction
	 */
	public final JPanel getPnlAction() {
	
		if(pnlAction == null) {
			pnlAction = new JPanel();
			pnlAction.setLayout(new FlowLayout(FlowLayout.TRAILING, 5, 5));
			pnlAction.add(getBtnCancel());
			pnlAction.add(getBtnOK());
		}
		return pnlAction;
	}
	
	/**
	 * @return {@link TreeSelectionModel}
	 */
	public final TreeSelectionModel getSelectionMode() {
	
		return getFileTreePanel().getSelectionMode();
	}
	
	/**
	 * @param selectionModel
	 */
	public final void setSelectionMode(final TreeSelectionModel selectionModel) {
	
		getFileTreePanel().setSelectionMode(selectionModel.getSelectionMode());
	}
}
